﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace WCFPortas
{
    public partial class Portas : System.Web.UI.Page
    {

        private string Ordenacao
        {
            get { return Convert.ToString(ViewState["ordenacao"]).Trim(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    
                    this.preencherForm(string.Empty, string.Empty, string.Empty);
                }
            }
            catch (Exception ex)
            {
                this.apresentarMensagem(ex.Message);
            }
        }

        private void preencherForm(string idModulo, string idSubmodulo, string sortExpression)
        {
            preencherGrid(idModulo, idSubmodulo, sortExpression);
            preencherDropModulos();
            inicializarScripts();
        }

        private void inicializarScripts()
        {
            this.btnLimpar.Attributes.Add("onclick", "javascript:novo(); return false;");
        }

        private void preencherGrid(string idModulo, string idSubmodulo, string sortExpression)
        {
            Sinacor.Tools.Negocio.Porta modulo = new Sinacor.Tools.Negocio.Porta();
            this.grdPortas.DataSource = modulo.getPorts(idModulo, idSubmodulo, sortExpression);
            this.grdPortas.DataBind();
        }

        private void preencherDropModulos()
        {
            Sinacor.Tools.Negocio.Porta modulo = new Sinacor.Tools.Negocio.Porta();
            this.ddlModulos.DataTextField = "Nome";
            this.ddlModulos.DataValueField = "Nome";
            List<Sinacor.Tools.Entidade.Modulo> modulos = modulo.getModulos();
            Sinacor.Tools.Entidade.Modulo.PropriedadeSort = "Nome";
            modulos.Sort(new Sinacor.Tools.Entidade.Modulo());
            this.ddlModulos.DataSource = modulos;
            this.ddlModulos.DataBind();
            this.ddlModulos.Items.Insert(0, string.Empty);
            this.ddlSubModulos.Items.Insert(0, string.Empty);
            this.preencherDropModulosI(modulos);
        }

        private void preencherDropModulosI(List<Sinacor.Tools.Entidade.Modulo> pModulos)
        {
            this.ddlModuloI.DataTextField = "Nome";
            this.ddlModuloI.DataValueField = "Nome";
            this.ddlModuloI.DataSource = pModulos;
            this.ddlModuloI.DataBind();
            this.ddlModuloI.Items.Insert(0, string.Empty);
        }

        private void preencherDropSubModulos(string idModulo)
        {
            Sinacor.Tools.Negocio.Porta modulo = new Sinacor.Tools.Negocio.Porta();
            this.ddlSubModulos.Items.Clear();
            this.ddlSubModulos.DataTextField = "Submodulo";
            this.ddlSubModulos.DataValueField = "Submodulo";
            this.ddlSubModulos.DataSource = modulo.getSubmodulos(idModulo);
            this.ddlSubModulos.DataBind();
            this.ddlSubModulos.Items.Insert(0, string.Empty);
        }

        protected void ddlModulos_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.preencherDropSubModulos(this.ddlModulos.SelectedValue);
                this.grdPortas.PageIndex = 0;
                this.preencherGrid(this.ddlModulos.SelectedValue, string.Empty, Ordenacao);
            }
            catch (Exception ex)
            {
                this.apresentarMensagem(ex.Message);
            }
        }

        protected void ddlSubModulos_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.preencherGrid(this.ddlModulos.SelectedValue, this.ddlSubModulos.SelectedValue, Ordenacao);
            }
            catch (Exception ex)
            {
                this.apresentarMensagem(ex.Message);
            }
        }

        protected void grdPortas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.grdPortas.PageIndex = e.NewPageIndex;
                this.preencherGrid(this.ddlModulos.SelectedValue, this.ddlSubModulos.SelectedValue, Ordenacao);
            }
            catch (Exception ex)
            {
                this.apresentarMensagem(ex.Message);
            }            
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                //armazena o módulo selecionado
                this.efetuarOperacao();
                this.preencherGrid(this.ddlModuloI.SelectedValue, this.txtSubModulo.Text, Ordenacao);
                this.ddlModulos.SelectedValue = this.ddlModuloI.SelectedValue;
                this.preencherDropSubModulos(this.ddlModulos.SelectedValue);
                this.ddlSubModulos.SelectedValue = this.txtSubModulo.Text;
                limpar();
                
            }
            catch (Exception ex)
            {
                this.apresentarMensagem(ex.Message);
            }
        }

        private void limpar()
        {
            this.txtSubModulo.Text = string.Empty;
            this.txtServico.Text = string.Empty;
            this.ddlModuloI.SelectedValue = string.Empty;
            this.txtPortaMEX.Text = string.Empty;
            this.txtPortaTCP.Text = string.Empty;
            this.hdnModulo.Value = string.Empty;
            this.hdnServico.Value = string.Empty;
            this.hdnSubmodulo.Value = string.Empty;
        }


        private void efetuarOperacao()
        {
            //if (this.hdnModulo.Value != string.Empty && this.hdnServico.Value != string.Empty && this.hdnSubmodulo.Value != string.Empty)
            if (this.hdnModulo.Value != string.Empty && this.hdnSubmodulo.Value != string.Empty)
            {
                this.alterar();
            }
            else
            {
                this.salvar();
            }
        }


        private void salvar()
        {
            Sinacor.Tools.Entidade.Modulo m = new Sinacor.Tools.Entidade.Modulo();
            m.Nome = this.ddlModuloI.SelectedValue;
            m.Servico = this.txtServico.Text.Trim();
            m.Submodulo = this.txtSubModulo.Text.Trim();
            Sinacor.Tools.Negocio.Porta md = new Sinacor.Tools.Negocio.Porta();
            md.incluir(m);
            this.hdnPorta.Value = m.Porta.ToString();
            this.hdnPortaMex.Value = m.PortaMex.ToString();          
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "porta", "setarValorPortas(" + this.hdnPorta.Value + ", " + this.hdnPortaMex.Value + ");", true);
        }

        private void alterar()
        {
            Sinacor.Tools.Entidade.Modulo moduloDestino = new Sinacor.Tools.Entidade.Modulo();
            Sinacor.Tools.Entidade.Modulo moduloOrigem = new Sinacor.Tools.Entidade.Modulo();
            moduloDestino.Nome = this.ddlModuloI.SelectedValue;
            moduloDestino.Servico = this.txtServico.Text.Trim();
            moduloDestino.Submodulo = this.txtSubModulo.Text.Trim();

            moduloOrigem.Nome = this.hdnModulo.Value;
            moduloOrigem.Servico = this.hdnServico.Value;
            moduloOrigem.Submodulo = this.hdnSubmodulo.Value;
                       
            Sinacor.Tools.Negocio.Porta moduloNegocio = new Sinacor.Tools.Negocio.Porta();
            moduloNegocio.atualizar(moduloOrigem, moduloDestino);

        }

        private void apresentarMensagem(string mensagem)
        {
            mensagem = mensagem.Replace("'", string.Empty).Replace(Environment.NewLine, string.Empty);
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "mensagem", "alert('" + mensagem + "');", true);
        }


       
        protected void grdPortas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            //contempla os casos conhecidos
            setRowStyles(e);
        }

        private void setRowStyles(GridViewRowEventArgs e)
        {
            Sinacor.Tools.Entidade.Modulo detalhe = (Sinacor.Tools.Entidade.Modulo)e.Row.DataItem;
            if (detalhe != null)
            {
                e.Row.CssClass = detalhe.Nome.ToUpper();
                e.Row.Attributes.Add("onmouseover", "this.className = 'HOVER';");
                e.Row.Attributes.Add("onmouseout", "this.className = '" + detalhe.Nome.ToUpper() + "';");
                e.Row.Attributes.Add("onclick", "javascript:setUpdateItem(this);");
                this.setGridCellStyle(e.Row, false);
            }
            else
            {
                e.Row.CssClass = "CABECALHO";
                this.setGridCellStyle(e.Row, true);
            }
            
        }

        private void setGridCellStyle(GridViewRow linha, bool whiteText)
        {
            int qtdeLoops = linha.Cells.Count;
            for (int i = 0; i < qtdeLoops ; i++)
            {
                linha.Cells[i].BorderColor = System.Drawing.Color.Black;
                linha.Cells[i].ForeColor = whiteText ? System.Drawing.Color.White : linha.Cells[i].ForeColor;
            }
        }

        protected void grdPortas_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["ordenacao"] = e.SortExpression;
            this.preencherGrid(this.ddlModulos.SelectedValue, this.ddlSubModulos.SelectedValue, Ordenacao);

        }
    }
}
